<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: add.phtml 5251 2010-08-31 09:15:55Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php
$this->headTitle($this->translate('file_edit_page_title'));
$this->headLink()->appendStylesheet($this->APP_STATIC_SERVER . '/skins/admin/default/css/form.css')
        ->appendStylesheet($this->APP_STATIC_SERVER . '/skins/admin/default/css/filelist.css');
$this->headScript()->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate.min.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/tinymce/tiny_mce.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.json-2.3.min.js')
        ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.base64.min.js');
?>
<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/article.png'; ?>" alt=""/>
    <span><?php echo $this->translate('file_add_page_title') ?></span>
</div>
<?php
$massage = $this->flashMessenger();
if ($massage) {
    ?>
    <div id="message"><?php echo $massage; ?></div>
<?php } ?>

<form method="post" action="<?php echo $this->url(array(), 'multimedia_file_edit'); ?>" id="editFileForm">
    <input type="hidden" id="id" name="id" value="<?php echo $this->file->id?>"/>
    <input type="hidden" id="file_type" name="file_type" value="<?php echo $this->file->file_type?>"/>
    <h3><?php echo $this->translate('file_edit_guide'); ?></h3>
    <div lass="r-left" style="width:60%; float:left;">


        <div class="row">
            <label><?php echo $this->translate('file_title'); ?>:</label>
            <input type="text" id="title" name="title" style="width: 420px" value="<?php echo $this->file->title?>"/>
            
        </div>
        <div class="row">
            <label><?php echo $this->translator('file_image_altertext'); ?>:</label>
            <input type="text" name="image_altertext" id="image_altertext" size="25"/>
        </div>
        <div class="row">
            <label><?php echo $this->translate('file_description'); ?>:</label>
            <textarea name="description" id="description" style="width: 420px; height: 250px"><?php echo $this->file->description?></textarea>
        </div>

       


    </div>
    <div class="r-right" style="width:35%;float:right;">
         <!-- Upload photo -->
        <p class="upload-guide">
             <?php echo $this->translator('file_upload_guide');?>
        </p>
        <div class="clearfix ">
            <div class="clearfix">
                <label><?php echo $this->translate('file_upload_image_guide'); ?>:</label>
                <?php
                echo $this->helperLoader('upload')->uploader('multimedia', array('id' => $this->file->id, 'extension' => '*.jpg;*.jpeg;*.png;*.gif;*.txt;*.psd;*.zip;*.rar;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx', 'multi' => true, 'auto' => true, 'simUploadLimit' => 1, 'sizeLimit' => 1024 * 1024 * 5), array('onComplete' => 'onUploadAttachmentComplete', 'onError' => 'onUploadError'));
                ?>
            </div>

            <div class="clearfix ">
                <label>&nbsp;</label>
                <div>
                    <input type="hidden" id="image" name="image" value="<?php echo $this->file->file_type == 'image' ? $this->imageData :'' ?>"/>
                    <div id="fileImage" >
                        <?php if($this->file->file_type == 'image'){?>
                        <img src="<?php echo $this->file->image_crop?>" alt="<?php echo $this->file->image_altertext?>" />
                        <?php }?>  
                        
                    </div>
                </div>
                <div id="attachments" style="width: 300px;">
                    
                    <ul>
                        <?php 
                        if($this->file->file_type != 'image'){
                                             
                        ?>
                        <li class="<?php echo $this->file->file_type ?>" id="attachment-<?php echo $this->file->id?>">
                            <?php echo substr($this->file->url, strrpos($this->file->url, '/') + 1)?>
                        </li>
                        <?php
                        }
                        ?>
                    </ul>
                    <input type="hidden" id="attachment" value="<?php echo $this->fileData?>"  name="attachment"/>
                </div>
            </div>
            
        </div>
        <?php //Tomato_Hook_Registry::getInstance()->executeAction('Multimedia_File_Add_ShowSidebar'); ?>
    </div>
    <div class="clearfix">
        <button class="button" type="submit"><span><?php echo $this->translate('file_edit_save_button'); ?></span></button>
    </div>
</div>




</form>

<script type="text/javascript">
    /**
     * Add TinyMCE Editor for set description input 
     * @since 2.0.2
     */
    tinyMCE.init({
        theme: 'simple', // 'advanced'
        theme_simple_toolbar_location : "top",
        theme_simple_toolbar_align : "left",
        width: '420',
        mode: 'exact',
        elements: 'description',
        relative_urls: false,
        remove_script_host: false
    });

    /**
     * Upload handlers
     */
    function onUploadError(event,ID,fileObj,errorObj) {
        alert(errorObj.type + ' Error: ' + errorObj.info);
    }
    //for upload attachment
    function onUploadAttachmentComplete(event, ID, fileObj, response, data) {
        $('#image').val('');
        $('#attachment').val('');
        $('#attachments ul').html('');
        var result = $.evalJSON(response);
        if(result.status){
            if(result.file.type == 'image'){
                $('#fileImage').html('');
                $('<img src="' + result.crop.url + '" width="270px" height="200px" />').appendTo($('#fileImage'));
                var urls = {};
                for (var thumb in result) {
                    urls[thumb] = result[thumb].url; 
                }
                urls['id'] = result.file.id;
                $('#image').val($.base64.encode($.toJSON(urls)));
                $('#file_type').val('image');
            }else{
                $('#attachments ul').append(
                    '<li id="attachment-' + result.file.id + '" class="' + result.file.type + '">'
                    + result.file.name
                    + '<span><a class="a_helper_action_delete" href="" onclick="removeAttachment(' + result.file.id + '); return false;"></a></span>'
                    +'</li>'

                );
                var attachment = {id: result.file.id, name: result.file.name, url: result.file.url, type: result.file.type};
                $('#attachment').val($.base64.encode($.toJSON(attachment)));
                $('#file_type').val(result.file.type);
                    
            }
            
            
        }
    }
    function removeAttachment(id){
        $.ajax({
            url: '<?php echo $this->url(array(), 'multimedia_file_delete') ?>',
            data : {id : id},
            type: 'POST',
            success: function(response){
                alert(response);
                var result = $.evalJSON(response);
                if(result.status){
                    var attachments = $.evalJSON($.base64.decode($('#attachment').val()));
                    for(var i = 0; i < attachments.length; i++){
                        if(attachments[i].id == id){
                            attachments.splice(i,1);
                            $("#attachment-" + id).fadeOut('slow');
                        }
                    }
                    $('#attachment').val($.base64.encode($.toJSON(attachments)));

                }
            }
        });
        return false;
    }

    $(document).ready(function() {
        /**
         * Select file type handler
         */
        $('#fileType').change(function() {
            $('#videoFile').hide();
            $('#' + $(this).val() + 'File').show();
        });

        /**
         * Validate form
         */
        $('#addFileForm').validate({
            rules: {
                title: {
                    required: true
                }
            },
            messages: {
                title: {
                    required: "<?php echo $this->translate('file_title_required'); ?>"
                }
            }
        });
    });
</script>
